package o;

import android.util.Log;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.ImageHeaderParser;
import com.bumptech.glide.load.ImageHeaderParser.ImageType;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;

public final class wp implements ImageHeaderParser {
    private static final int[] ˊ = new int[]{0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8};
    static final byte[] ˎ = "Exif\u0000\u0000".getBytes(Charset.forName("UTF-8"));

    interface c {
        int ˊ() throws IOException;

        int ˊ(byte[] bArr, int i) throws IOException;

        long ˎ(long j) throws IOException;

        short ˎ() throws IOException;

        int ॱ() throws IOException;
    }

    static final class a implements c {
        private final InputStream ˏ;

        a(InputStream inputStream) {
            this.ˏ = inputStream;
        }

        public int ॱ() throws IOException {
            return ((this.ˏ.read() << 8) & 65280) | (this.ˏ.read() & 255);
        }

        public short ˎ() throws IOException {
            return (short) (this.ˏ.read() & 255);
        }

        public long ˎ(long j) throws IOException {
            if (j < 0) {
                return 0;
            }
            long j2 = j;
            while (j2 > 0) {
                long skip = this.ˏ.skip(j2);
                if (skip > 0) {
                    j2 -= skip;
                } else if (this.ˏ.read() == -1) {
                    break;
                } else {
                    j2--;
                }
            }
            return j - j2;
        }

        public int ˊ(byte[] bArr, int i) throws IOException {
            int i2 = i;
            while (i2 > 0) {
                int read = this.ˏ.read(bArr, i - i2, i2);
                if (read == -1) {
                    break;
                }
                i2 -= read;
            }
            return i - i2;
        }

        public int ˊ() throws IOException {
            return this.ˏ.read();
        }
    }

    static final class b {
        private final ByteBuffer ˊ;

        b(byte[] bArr, int i) {
            this.ˊ = (ByteBuffer) ByteBuffer.wrap(bArr).order(ByteOrder.BIG_ENDIAN).limit(i);
        }

        void ˊ(ByteOrder byteOrder) {
            this.ˊ.order(byteOrder);
        }

        int ˎ() {
            return this.ˊ.remaining();
        }

        int ˊ(int i) {
            return ˎ(i, 4) ? this.ˊ.getInt(i) : -1;
        }

        short ˎ(int i) {
            return ˎ(i, 2) ? this.ˊ.getShort(i) : (short) -1;
        }

        private boolean ˎ(int i, int i2) {
            return this.ˊ.remaining() - i >= i2;
        }
    }

    static final class d implements c {
        private final ByteBuffer ˋ;

        d(ByteBuffer byteBuffer) {
            this.ˋ = byteBuffer;
            byteBuffer.order(ByteOrder.BIG_ENDIAN);
        }

        public int ॱ() {
            return ((ˊ() << 8) & 65280) | (ˊ() & 255);
        }

        public short ˎ() {
            return (short) (ˊ() & 255);
        }

        public long ˎ(long j) {
            int min = (int) Math.min((long) this.ˋ.remaining(), j);
            this.ˋ.position(this.ˋ.position() + min);
            return (long) min;
        }

        public int ˊ(byte[] bArr, int i) {
            int min = Math.min(i, this.ˋ.remaining());
            if (min == 0) {
                return -1;
            }
            this.ˋ.get(bArr, 0, min);
            return min;
        }

        public int ˊ() {
            if (this.ˋ.remaining() < 1) {
                return -1;
            }
            return this.ˋ.get();
        }
    }

    @NonNull
    public ImageType ˏ(@NonNull InputStream inputStream) throws IOException {
        return ॱ(new a((InputStream) aas.ˎ(inputStream)));
    }

    @NonNull
    public ImageType ˎ(@NonNull ByteBuffer byteBuffer) throws IOException {
        return ॱ(new d((ByteBuffer) aas.ˎ(byteBuffer)));
    }

    public int ˎ(@NonNull InputStream inputStream, @NonNull ud udVar) throws IOException {
        return ॱ(new a((InputStream) aas.ˎ(inputStream)), (ud) aas.ˎ(udVar));
    }

    @NonNull
    private ImageType ॱ(c cVar) throws IOException {
        int ॱ = cVar.ॱ();
        if (ॱ == 65496) {
            return ImageType.JPEG;
        }
        ॱ = ((ॱ << 16) & -65536) | (cVar.ॱ() & 65535);
        if (ॱ == -1991225785) {
            cVar.ˎ(21);
            return cVar.ˊ() >= 3 ? ImageType.PNG_A : ImageType.PNG;
        } else if ((ॱ >> 8) == 4671814) {
            return ImageType.GIF;
        } else {
            if (ॱ != 1380533830) {
                return ImageType.UNKNOWN;
            }
            cVar.ˎ(4);
            if ((((cVar.ॱ() << 16) & -65536) | (cVar.ॱ() & 65535)) != 1464156752) {
                return ImageType.UNKNOWN;
            }
            ॱ = ((cVar.ॱ() << 16) & -65536) | (cVar.ॱ() & 65535);
            if ((ॱ & -256) != 1448097792) {
                return ImageType.UNKNOWN;
            }
            if ((ॱ & 255) == 88) {
                cVar.ˎ(4);
                return (cVar.ˊ() & 16) != 0 ? ImageType.WEBP_A : ImageType.WEBP;
            } else if ((ॱ & 255) != 76) {
                return ImageType.WEBP;
            } else {
                cVar.ˎ(4);
                return (cVar.ˊ() & 8) != 0 ? ImageType.WEBP_A : ImageType.WEBP;
            }
        }
    }

    private int ॱ(c cVar, ud udVar) throws IOException {
        int ॱ = cVar.ॱ();
        if (ˋ(ॱ)) {
            ॱ = ˊ(cVar);
            if (ॱ != -1) {
                byte[] bArr = (byte[]) udVar.ॱ(ॱ, byte[].class);
                try {
                    ॱ = ˎ(cVar, bArr, ॱ);
                    return ॱ;
                } finally {
                    udVar.ˊ(bArr);
                }
            } else if (!Log.isLoggable("DfltImageHeaderParser", 3)) {
                return -1;
            } else {
                Log.d("DfltImageHeaderParser", "Failed to parse exif segment length, or exif segment not found");
                return -1;
            }
        } else if (!Log.isLoggable("DfltImageHeaderParser", 3)) {
            return -1;
        } else {
            Log.d("DfltImageHeaderParser", "Parser doesn't handle magic number: " + ॱ);
            return -1;
        }
    }

    private int ˎ(c cVar, byte[] bArr, int i) throws IOException {
        int ˊ = cVar.ˊ(bArr, i);
        if (ˊ != i) {
            if (!Log.isLoggable("DfltImageHeaderParser", 3)) {
                return -1;
            }
            Log.d("DfltImageHeaderParser", "Unable to read exif segment data, length: " + i + ", actually read: " + ˊ);
            return -1;
        } else if (ˊ(bArr, i)) {
            return ॱ(new b(bArr, i));
        } else {
            if (!Log.isLoggable("DfltImageHeaderParser", 3)) {
                return -1;
            }
            Log.d("DfltImageHeaderParser", "Missing jpeg exif preamble");
            return -1;
        }
    }

    private boolean ˊ(byte[] bArr, int i) {
        boolean z = bArr != null && i > ˎ.length;
        if (z) {
            for (int i2 = 0; i2 < ˎ.length; i2++) {
                if (bArr[i2] != ˎ[i2]) {
                    return false;
                }
            }
        }
        return z;
    }

    private int ˊ(c cVar) throws IOException {
        long ˎ;
        int ॱ;
        do {
            short ˎ2;
            short ˎ3 = cVar.ˎ();
            if (ˎ3 == (short) 255) {
                ˎ2 = cVar.ˎ();
                if (ˎ2 == (short) 218) {
                    return -1;
                }
                if (ˎ2 != (short) 217) {
                    ॱ = cVar.ॱ() - 2;
                    if (ˎ2 == (short) 225) {
                        return ॱ;
                    }
                    ˎ = cVar.ˎ((long) ॱ);
                } else if (!Log.isLoggable("DfltImageHeaderParser", 3)) {
                    return -1;
                } else {
                    Log.d("DfltImageHeaderParser", "Found MARKER_EOI in exif segment");
                    return -1;
                }
            } else if (!Log.isLoggable("DfltImageHeaderParser", 3)) {
                return -1;
            } else {
                Log.d("DfltImageHeaderParser", "Unknown segmentId=" + ˎ3);
                return -1;
            }
        } while (ˎ == ((long) ॱ));
        if (!Log.isLoggable("DfltImageHeaderParser", 3)) {
            return -1;
        }
        Log.d("DfltImageHeaderParser", "Unable to skip enough data, type: " + ˎ2 + ", wanted to skip: " + ॱ + ", but actually skipped: " + ˎ);
        return -1;
    }

    private static int ॱ(b bVar) {
        ByteOrder byteOrder;
        int length = "Exif\u0000\u0000".length();
        short ˎ = bVar.ˎ(length);
        switch (ˎ) {
            case (short) 18761:
                byteOrder = ByteOrder.LITTLE_ENDIAN;
                break;
            case (short) 19789:
                byteOrder = ByteOrder.BIG_ENDIAN;
                break;
            default:
                if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                    Log.d("DfltImageHeaderParser", "Unknown endianness = " + ˎ);
                }
                byteOrder = ByteOrder.BIG_ENDIAN;
                break;
        }
        bVar.ˊ(byteOrder);
        length += bVar.ˊ(length + 4);
        short ˎ2 = bVar.ˎ(length);
        for (ˎ = (short) 0; ˎ < ˎ2; ˎ++) {
            int ˏ = ˏ(length, ˎ);
            short ˎ3 = bVar.ˎ(ˏ);
            if (ˎ3 == (short) 274) {
                short ˎ4 = bVar.ˎ(ˏ + 2);
                if (ˎ4 >= (short) 1 && ˎ4 <= (short) 12) {
                    int ˊ = bVar.ˊ(ˏ + 4);
                    if (ˊ >= 0) {
                        if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                            Log.d("DfltImageHeaderParser", "Got tagIndex=" + ˎ + " tagType=" + ˎ3 + " formatCode=" + ˎ4 + " componentCount=" + ˊ);
                        }
                        ˊ += ˊ[ˎ4];
                        if (ˊ <= 4) {
                            ˏ += 8;
                            if (ˏ < 0 || ˏ > bVar.ˎ()) {
                                if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                                    Log.d("DfltImageHeaderParser", "Illegal tagValueOffset=" + ˏ + " tagType=" + ˎ3);
                                }
                            } else if (ˊ >= 0 && ˏ + ˊ <= bVar.ˎ()) {
                                return bVar.ˎ(ˏ);
                            } else {
                                if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                                    Log.d("DfltImageHeaderParser", "Illegal number of bytes for TI tag data tagType=" + ˎ3);
                                }
                            }
                        } else if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                            Log.d("DfltImageHeaderParser", "Got byte count > 4, not orientation, continuing, formatCode=" + ˎ4);
                        }
                    } else if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                        Log.d("DfltImageHeaderParser", "Negative tiff component count");
                    }
                } else if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                    Log.d("DfltImageHeaderParser", "Got invalid format code = " + ˎ4);
                }
            }
        }
        return -1;
    }

    private static int ˏ(int i, int i2) {
        return (i + 2) + (i2 * 12);
    }

    private static boolean ˋ(int i) {
        return (i & 65496) == 65496 || i == 19789 || i == 18761;
    }
}
